package com.gxuwz.business.dao.impl;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.Medicine;
import com.gxuwz.business.entity.Student;
import com.gxuwz.util.Pager;

@Repository("medicineDAO")
public class MedicineDAO extends BaseDaoImpl<MedicineDAO>{

	/**
	 * 分页查询
	 * @param medicine
	 * @param page
	 * @param limit
	 * @return
	 */
	public Pager<Medicine> getByPager(Medicine medicine, Integer page, Integer limit) {
		String hql = "from Medicine where 1=1 and status = 0";
		//模糊查询
		if(null != medicine.getMedSpec() && !"".equals(medicine.getMedSpec())){
			String key = medicine.getMedSpec();
			hql += " and (medId like '%"+key+"%' or medName like '%"+key+"%' or medSpec like '%"+key+"%' "
					+ "or medCount like '%"+key+"%' or medFuntion like '%"+key+"%' or medPrice like '%"+key+"%' "
							+ "or medTime like '%"+key+"%' or medAddress like '%"+key+"%' or medType like '%"+key+"%')";
		}
		return super.find(hql, null, null, (page-1)*limit, limit);
	}
	
	/**
	 * 根据药品编号查询
	 */
	public Medicine findById(Medicine medicine){
		String hql = "from Medicine where medId = '"+medicine.getMedId()+"'";
		try {
			return (Medicine) getHibernateTemplate().find(hql, null).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 添加药品信息
	 */
	public boolean add(Medicine medicine){
		try{
			getHibernateTemplate().save(medicine);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 检测药品信息是否已存在
	 */
	public boolean checkMedName(Medicine medicine){
		return checkExist(Medicine.class, "medName", medicine.getMedName());
	}
	
	/**
	 * 伪删除
	 */
	public boolean delete(Medicine medicine){
		boolean status = false;
		String hql = "update Medicine set status = 1 where medId = '"+medicine.getMedId()+"'";
		try{
			getHibernateTemplate().bulkUpdate(hql);
			status = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return status;   
	}
	
	/**
	 * 只更新药品名称、服用方式、药品规格、药品作用、药品价格、生产日期、生产地址、药品类型
	 */
	public boolean update(Medicine s){
		boolean isSucceed = false;
		String hql = "update Medicine set medName=?, medSpec=?, medCount=?, medFuntion=?, medPrice=?, medTime=?"
				+ ",medAddress=?,medType=? where medId=?";
		try {
			int count = super.getHibernateTemplate().bulkUpdate(hql, s.getMedName(),s.getMedSpec(),s.getMedCount(),s.getMedFuntion()
					,s.getMedPrice(),s.getMedTime(),s.getMedAddress(),s.getMedType(), s.getMedId());
//			System.out.println("更改了"+count+"条数据");
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 查询药名
	 */
	public String getMedName(String medId){
		String hql = "select medName from Medicine where medId = ?";
		try {
			return (String) getHibernateTemplate().find(hql, medId).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查询单价
	 */
	public Double getMedPrice(String medId){
		String hql = "select medPrice from Medicine where medId = ?";
		try {
			return (Double) getHibernateTemplate().find(hql, medId).get(0);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	

}
